package com.adinnet.common.jpush;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class JPushService {

    private static final Logger log = LoggerFactory.getLogger(JPushService.class);

    private Boolean apns = true;

    /**
     * 广播 (所有平台，所有设备, 不支持附加信息)
     *
     * @param pushBean 推送内容
     * @return
     * @author YangJie [2016年6月17日 下午4:12:08]
     */
    public boolean pushAll(PushBean pushBean) {
        return sendPush(pushBean.getAppKey(), pushBean.getMasterSecret(), PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setNotification(Notification.alert(pushBean.getAlert()))
                .setOptions(Options.newBuilder().setApnsProduction(apns).build())
                .build());
    }

    /**
     * ios广播
     *
     * @param pushBean 推送内容
     * @return
     * @author YangJie [2016年6月17日 下午3:59:21]
     */
    public boolean pushIos(PushBean pushBean) {
        return sendPush(pushBean.getAppKey(), pushBean.getMasterSecret(), PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.all())
                .setNotification(Notification.ios(pushBean.getAlert(), pushBean.getExtras()))
                .setOptions(Options.newBuilder().setApnsProduction(apns).build())
                .build());
    }

    /**
     * ios通过registid推送 (一次推送最多 1000 个)
     *
     * @param pushBean  推送内容
     * @param registids 推送id
     * @return
     * @author YangJie [2016年6月17日 下午3:59:21]
     */
    public boolean pushIos(Boolean env,PushBean pushBean, String... registids) {
        return sendPush(pushBean.getAppKey(), pushBean.getMasterSecret(), PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.registrationId(registids))
                .setNotification(Notification.ios(pushBean.getAlert(), pushBean.getExtras()))
                .setOptions(Options.newBuilder().setApnsProduction(env).build())
                .build());
    }


    /**
     * android广播
     *
     * @param pushBean 推送内容
     * @return
     * @author YangJie [2016年6月17日 下午3:59:21]
     */
    public boolean pushAndroid(PushBean pushBean) {
        return sendPush(pushBean.getAppKey(), pushBean.getMasterSecret(), PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras()))
                .setOptions(Options.newBuilder().setApnsProduction(apns).build())
                .build());
    }

    /**
     * android通过registid推送 (一次推送最多 1000 个)
     *
     * @param pushBean  推送内容
     * @param registids 推送id
     * @return
     * @author YangJie [2016年6月17日 下午3:59:21]
     */
    public boolean pushAndroid(Boolean env,PushBean pushBean, String... registids) {
        return sendPush(pushBean.getAppKey(), pushBean.getMasterSecret(), PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(registids))
                .setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras()))
                .setOptions(Options.newBuilder().setApnsProduction(env).build())
                .build());
    }

    /**
     * 调用api推送
     *
     * @param pushPayload 推送实体
     * @return
     * @author YangJie [2016年6月17日 下午4:19:03]
     */
    private boolean sendPush(String appKey, String masterSecret, PushPayload pushPayload) {
        log.info("发送极光推送请求: " + pushPayload);
        PushResult result = null;
        JPushClient jPushClient = new JPushClient(masterSecret, appKey);
        try {
            result = jPushClient.sendPush(pushPayload);
        } catch (APIConnectionException e) {
            log.info(e.getMessage());
        } catch (APIRequestException e) {
            log.info(e.getMessage());
        } finally {
            jPushClient.close();
        }
        if (result != null && result.isResultOK()) {
            log.info("推送成功" + result);
            return true;
        } else {
            log.info("推送失败" + result);
            return false;
        }
    }
}
